﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class ManualToolMnt_Insert : Form
    {
        int i;
        string 手工具編號, 手工具名稱, 規格型號, 材料編號, 廠牌代號, 請購人員代號,
               工具類別代號, 存放地點代號, 數量, 備註;
        object 照片;
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        public ManualToolMnt_Insert(int ix,string 手工具編號,string 手工具名稱,string 規格型號,string 材料編號,string 廠牌代號,string 請購人員代號,
               string 工具類別代號, string 存放地點代號, object 照片, string 數量, string 備註)
        {
            InitializeComponent();
            this.i = ix;
            this.手工具編號 = 手工具編號;
            this.手工具名稱 = 手工具名稱;
            this.規格型號 = 規格型號;
            this.材料編號 = 材料編號;
            this.廠牌代號 = 廠牌代號;
            this.請購人員代號 = 請購人員代號;
            this.工具類別代號 = 工具類別代號;
            this.存放地點代號 = 存放地點代號;
            this.照片 = 照片;
            this.數量 = 數量;
            this.備註 = 備註;
        }

        private void bt_加附_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();

                dialog.Title = "開啟照片";
                dialog.Filter = "照片當 (*.jpg, *.jpeg, *.jpe, *.bmp, *.png) | *.jpg; *.jpeg; *.jpe; *.bmp; *.png"; ;//Bitmap, Icon,GIF,bmp,ico,gif,jpeg
                var result = dialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    String f = dialog.FileName;
                    Image img = Image.FromFile(f);
                    pb_照片.Image = img;
                }
                else return;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void bt_存檔_Click(object sender, EventArgs e)
        {
            if (i == 1)
            {
                try
                {
                    if (txt_手工具編號.Text.ToString() == null || txt_手工具編號.Text.ToString().Equals(""))
                        MessageBox.Show("資料還沒輸入資料 ！");
                    else
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0;
                        String sql = "SELECT COUNT(*) FROM 手工具 WHERE 手工具編號='" + txt_手工具編號.Text.ToString() + "'";
                        cmd0 = new SqlCommand(sql, con);
                        if ((int)cmd0.ExecuteScalar() > 0)
                        {
                            MessageBox.Show("Error! 資料由來");
                        }
                        else
                        {
                            cmd0 = new SqlCommand("sp_手工具_insert", con);
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                            cmd0.Parameters.AddWithValue("@手工具名稱", txt_手工具名稱.Text.ToString());
                            cmd0.Parameters.AddWithValue("@規格型號", txt_規格型號.Text.ToString());
                            cmd0.Parameters.AddWithValue("@廠牌", txt_廠牌.Text.ToString());
                            cmd0.Parameters.AddWithValue("@請購人員", txt_請購人員.Text.ToString());
                            cmd0.Parameters.AddWithValue("@材料編號", txt_材料編號.Text.ToString());
                            cmd0.Parameters.AddWithValue("@工具類別", txt_工具類別.Text.ToString());
                            cmd0.Parameters.AddWithValue("@入庫數量", Int32.Parse(txt_入庫數量.Text.ToString()));
                            if (txt_照片.Text.ToString() != "")
                            {
                                byte[] imageData = ReadFile(txt_照片.Text.ToString());//ReadFile tu viet o ben duoi
                                cmd0.Parameters.AddWithValue("@照片", (object)imageData);
                            }
                            cmd0.Parameters.AddWithValue("@存放地點", txt_存放地點.Text.ToString());
                            cmd0.Parameters.AddWithValue("@備註", txt_備註.Text.ToString());
                            cmd0.ExecuteNonQuery();
                            MessageBox.Show("新增成功");
                        }
                        con.Close();
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
            if (i == 2)
            {
                try
                {
                    if (txt_手工具編號.Text.ToString() == null || txt_手工具編號.Text.ToString().Equals(""))
                        MessageBox.Show("資料還沒輸入資料 ！");
                    else
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0;
                        String sql = "SELECT COUNT(*) FROM 手工具 WHERE 手工具編號='" + 手工具編號 + "'";
                        cmd0 = new SqlCommand(sql, con);
                        if ((int)cmd0.ExecuteScalar() > 0)
                        {
                            DialogResult answer = MessageBox.Show("您確定要修改資料 ?", "警報", MessageBoxButtons.YesNo);
                            if (answer == DialogResult.Yes)
                            {
                                cmd0 = new SqlCommand("sp_手工具_update", con);
                                cmd0.CommandType = CommandType.StoredProcedure;
                                cmd0.Parameters.AddWithValue("@手工具編號_1", 手工具編號);
                                cmd0.Parameters.AddWithValue("@手工具編號_2", txt_手工具編號.Text.ToString());
                                cmd0.Parameters.AddWithValue("@手工具名稱", txt_手工具名稱.Text.ToString());
                                cmd0.Parameters.AddWithValue("@規格型號", txt_規格型號.Text.ToString());
                                cmd0.Parameters.AddWithValue("@廠牌", txt_廠牌.Text.ToString());
                                cmd0.Parameters.AddWithValue("@請購人員", txt_請購人員.Text.ToString());
                                cmd0.Parameters.AddWithValue("@材料編號", txt_材料編號.Text.ToString());
                                cmd0.Parameters.AddWithValue("@工具類別", txt_工具類別.Text.ToString());
                                cmd0.Parameters.AddWithValue("@入庫數量", Int32.Parse(txt_入庫數量.Text.ToString()));

                                if (txt_照片.Text.ToString() != "")
                                {
                                    byte[] imageData = ReadFile(txt_照片.Text.ToString());//ReadFile tu viet o ben duoi
                                    cmd0.Parameters.AddWithValue("@照片", (object)imageData);
                                }
                                cmd0.Parameters.AddWithValue("@存放地點", txt_存放地點.Text.ToString());
                                cmd0.Parameters.AddWithValue("@備註", txt_備註.Text.ToString());
                                cmd0.ExecuteNonQuery();
                                MessageBox.Show("修改成功");
                            }    
                        }
                        else
                        {
                            MessageBox.Show("Error! 資料不由來");                     
                        }
                        con.Close();
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }         
        }

        byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            //When you use BinaryReader, you need to supply number of bytes 
            //to read from file.
            //In this case we want to read entire file. 
            //So supplying total number of bytes.
            data = br.ReadBytes((int)numBytes);
            return data;
        }

        private void bt_加附_Click_1(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();

                dialog.Title = "開啟照片";
                dialog.Filter = "照片當 (*.jpg, *.jpeg, *.jpe, *.bmp, *.png) | *.jpg; *.jpeg; *.jpe; *.bmp; *.png"; ;//Bitmap, Icon,GIF,bmp,ico,gif,jpeg
                var result = dialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txt_照片.Text = dialog.FileName;
                    Image img = Image.FromFile(txt_照片.Text.ToString());
                    pb_照片.Image = img;
                }
                else return;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        public Image byteArraytoImage(byte[] filesbyte)
        {
            using (System.IO.MemoryStream filestream = new System.IO.MemoryStream(filesbyte))
            {
                return Image.FromStream(filestream);
            }
        }

        private void ManualToolMnt_Insert_Load(object sender, EventArgs e)
        {
            if (i == 2)
            {
                this.Text = "手工具修改";
            }
            txt_手工具編號.Text = 手工具編號;
            txt_手工具名稱.Text = 手工具名稱;
            txt_規格型號.Text = 規格型號;
            txt_材料編號.Text = 材料編號;
            txt_廠牌.Text = 廠牌代號;
            txt_請購人員.Text = 請購人員代號;
            txt_工具類別.Text = 工具類別代號;
            txt_存放地點.Text = 存放地點代號;
            if (string.IsNullOrEmpty(照片.ToString())) pb_照片.Image = null;
            else pb_照片.Image = byteArraytoImage((byte[])照片);
            txt_入庫數量.Text = 數量;
            txt_備註.Text = 備註;
        }

        private void bt_離開_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_廠牌);
            frm.ShowDialog();
        }

        private void bt_存放地點_search_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_存放地點);
            frm.ShowDialog();
        }

        private void bt_工具類別_search_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_工具類別);
            frm.ShowDialog();
        }

        private void bt_請購人員_search_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_請購人員);
            frm.ShowDialog();
        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void txt_工具類別_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 手工具類別 where 手工具類別代號=N'{0}'", txt_工具類別.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_工具類別.Text = "";
                else lb_工具類別.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_存放地點_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 手工具存放地點 where 存放地點代號=N'{0}'", txt_存放地點.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_存放地點.Text = "";
                else lb_存放地點.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_廠牌_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 手工具廠牌 where 廠牌代號=N'{0}'", txt_廠牌.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_廠牌.Text = "";
                else lb_廠牌.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_請購人員_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 手工具請購人員 where 請購人員代號=N'{0}'", txt_請購人員.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_請購人員.Text = "";
                else lb_請購人員.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_入庫數量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("很抱歉，請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
